/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.DateTools;
import javacommon.util.Notes;
import javacommon.util.StringUtils;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.SysUserDao;
import com.tech15.core.model.SysUser;
import com.tech15.pub.dao.MsgTipsDao;
import com.tech15.pub.model.MsgTips;
import com.tech15.task.dao.ZdxzdcCljlDao;
import com.tech15.task.dao.ZdxzdcDao;
import com.tech15.task.model.Zdxzdc;
import com.tech15.task.model.ZdxzdcCljl;
import com.tech15.task.vo.query.ZdxzdcQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class ZdxzdcManager extends BaseManager<Zdxzdc,java.lang.String>{

	private ZdxzdcDao zdxzdcDao;
	private SysUserDao sysUserDao;
	private MsgTipsDao msgTipsDao;
	private ZdxzdcCljlDao zdxzdcCljlDao;
	
	public void setZdxzdcCljlDao(ZdxzdcCljlDao zdxzdcCljlDao) {
		this.zdxzdcCljlDao = zdxzdcCljlDao;
	}
	public void setMsgTipsDao(MsgTipsDao msgTipsDao) {
		this.msgTipsDao = msgTipsDao;
	}
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	public void setZdxzdcDao(ZdxzdcDao dao) {
		this.zdxzdcDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.zdxzdcDao;
	}
	
	public List<ZdxzdcCljl> searchWcjlById(String id){
		List<ZdxzdcCljl> cljlList = zdxzdcDao.searchWcjlById(id);
		return cljlList;
	}
	
	public Zdxzdc getById(String id){
		Zdxzdc zdxzdc = zdxzdcDao.getById(id);
		List<ZdxzdcCljl> cljlList = zdxzdcDao.searchJlById(id);
		zdxzdc.setCljlList(cljlList);
		return zdxzdc;
	}
	
	public ZdxzdcCljl getJlByClid(String jlid){
		return zdxzdcCljlDao.getById(jlid);
	}
	
	@Transactional(readOnly=true)
	public Page findPage(ZdxzdcQuery query) {
		return zdxzdcDao.findPage(query);
	}
	
	public void saveXzdc(Zdxzdc xzdc){
		if(xzdc.getLb() == 1){
			xzdc.setSpzt(0);
			xzdc.setZxzt(-1);
		}else{
			xzdc.setSpzt(1);
			xzdc.setZxzt(0);
			
			xzdc.setSpsj(DateTools.getCurrentTime());
			xzdc.setLdps("无");
			xzdc.setSpr(xzdc.getQcr());
			xzdc.setSprxm(xzdc.getQcrxm());
			xzdc.setFgld(xzdc.getQcr());
			xzdc.setFgldxm(xzdc.getQcrxm());
		}
		xzdc.setQcsj(DateTools.getCurrentTime());
		if(ObjectUtils.isNotEmpty(xzdc.getSpr())){
			SysUser user = sysUserDao.getById(xzdc.getSpr());
			xzdc.setSprxm(user.getRealname());
		}
		zdxzdcDao.save(xzdc);
		zdxzdcDao.flush();
		
		if(xzdc.getLb() == 1){
			MsgTips tips = new MsgTips();
			tips.setBt(xzdc.getXggz());
			tips.setContent(xzdc.getJtyq());
			tips.setTablename(Notes.zdxzdc);
			tips.setTableid(xzdc.getId()+"");
			tips.setZt(0);
			tips.setUserid(xzdc.getSpr());
			tips.setXxsj(DateTools.getCurrentTime());
			tips.setUrl(Notes.zdxzdcUrl+"id="+xzdc.getId());
			msgTipsDao.save(tips);
		}else{
			if(xzdc.getExecUsers() != null){
				for(Long userid : xzdc.getExecUsers()){
					ZdxzdcCljl cljl = new ZdxzdcCljl();
					cljl.setDcid(xzdc.getId());
					cljl.setOptact(1);
					cljl.setZt(0);
					cljl.setZxr(userid);
					cljl.setRwsj(DateTools.getCurrentTime());
					SysUser user = sysUserDao.getById(userid);
					cljl.setZxrxm(user.getRealname());
					zdxzdcCljlDao.save(cljl);
					
					MsgTips tips = new MsgTips();
					tips.setBt(xzdc.getXggz());
					tips.setContent(xzdc.getJtyq());
					tips.setTablename(Notes.zdxzdc);
					tips.setTableid(xzdc.getId()+"");
					tips.setZt(0);
					tips.setUserid(xzdc.getZxr());
					tips.setXxsj(DateTools.getCurrentTime());
					tips.setUrl(Notes.zdxzdcUrl+"id="+xzdc.getId());
					msgTipsDao.save(tips);
				}
				
				xzdc.setZxzt(0);
				zdxzdcDao.update(xzdc);
			}
		}
	}
	
	/**审核分配*/
	public void shfp(Zdxzdc xzdc){
		xzdc.setSpzt(1);
		xzdc.setSpsj(DateTools.getCurrentTime());
		if(ObjectUtils.isNotEmpty(xzdc.getFgld())){
			SysUser user = sysUserDao.getById(xzdc.getFgld());
			xzdc.setFgldxm(user.getRealname());
		}
		zdxzdcDao.update(xzdc);
		zdxzdcDao.flush();
		
		MsgTips tips = new MsgTips();
		tips.setBt(xzdc.getXggz());
		tips.setContent(xzdc.getJtyq());
		tips.setTablename(Notes.zdxzdc);
		tips.setTableid(xzdc.getId()+"");
		tips.setZt(0);
		tips.setUserid(xzdc.getFgld());
		tips.setXxsj(DateTools.getCurrentTime());
		tips.setUrl(Notes.zdxzdcUrl+"id="+xzdc.getId());
		msgTipsDao.save(tips);
	}
	
	/**执行分配*/
	public void zxfp(Zdxzdc xzdc){
		/*xzdc.setZxzt(0);
		if(ObjectUtils.isNotEmpty(xzdc.getZxr())){
			SysUser user = sysUserDao.getById(xzdc.getZxr());
			xzdc.setZxrxm(user.getRealname());
		}
		zdxzdcDao.update(xzdc);
		zdxzdcDao.flush();*/
		
		if(xzdc.getExecUsers() != null){
			for(Long userid : xzdc.getExecUsers()){
				ZdxzdcCljl cljl = new ZdxzdcCljl();
				cljl.setDcid(xzdc.getId());
				cljl.setOptact(1);
				cljl.setZt(0);
				cljl.setZxr(userid);
				cljl.setRwsj(DateTools.getCurrentTime());
				SysUser user = sysUserDao.getById(userid);
				cljl.setZxrxm(user.getRealname());
				zdxzdcCljlDao.save(cljl);
				
				MsgTips tips = new MsgTips();
				tips.setBt(xzdc.getXggz());
				tips.setContent(xzdc.getJtyq());
				tips.setTablename(Notes.zdxzdc);
				tips.setTableid(xzdc.getId()+"");
				tips.setZt(0);
				tips.setUserid(xzdc.getZxr());
				tips.setXxsj(DateTools.getCurrentTime());
				tips.setUrl(Notes.zdxzdcUrl+"id="+xzdc.getId());
				msgTipsDao.save(tips);
			}
			
			xzdc.setZxzt(2);
			zdxzdcDao.update(xzdc);
		}
	}
	/**执行处理*/
	public void zxcl(Zdxzdc xzdc){
		if(StringUtils.notNull(xzdc.getJlid())){
			ZdxzdcCljl cljl = zdxzdcCljlDao.getById(xzdc.getJlid());
			if(cljl != null){
				cljl.setOptact(1);
				cljl.setZt(3);
				cljl.setWcsj(DateTools.getCurrentTime());
				cljl.setIsywc(xzdc.getIsywc());
				cljl.setWcqk(xzdc.getWcqk());
				cljl.setXygzjh(xzdc.getXygzjh());
				zdxzdcCljlDao.update(cljl);
				
				/*xzdc.setZxzt(3);
				zdxzdcDao.update(xzdc);
				zdxzdcDao.flush();
				
				MsgTips tips = new MsgTips();
				tips.setBt(xzdc.getXggz());
				tips.setContent(xzdc.getJtyq());
				tips.setTablename(Notes.zdxzdc);
				tips.setTableid(xzdc.getId()+"");
				tips.setZt(0);
				tips.setUserid(xzdc.getFgld());
				tips.setXxsj(DateTools.getCurrentTime());
				tips.setUrl(Notes.zdxzdcUrl+"id="+xzdc.getId());
				msgTipsDao.save(tips);*/
			}
		}
	}
	/**执行审核*/
	public void zxsh(Zdxzdc xzdc){
		/*xzdc.setZxzt(1);
		zdxzdcDao.update(xzdc);
		zdxzdcDao.flush();*/
		
		if(StringUtils.notNull(xzdc.getJlid())){
			ZdxzdcCljl cljl = zdxzdcCljlDao.getById(xzdc.getJlid());
			if(xzdc.getIsywc() == 0){
				ZdxzdcCljl cljl_new = new ZdxzdcCljl();
				cljl.setOptact(1);
				cljl_new.setDcid(xzdc.getId());
				cljl_new.setZt(0);
				cljl_new.setZxr(cljl.getZxr());
				cljl_new.setZxrxm(cljl.getZxrxm());
				cljl_new.setRwsj(DateTools.getCurrentTime());
				zdxzdcCljlDao.save(cljl_new);
			}
			cljl.setZt(1);
			cljl.setWcqk(xzdc.getWcqk());
			cljl.setXygzjh(xzdc.getXygzjh());
			cljl.setWcsj(DateTools.getCurrentTime());
			zdxzdcCljlDao.update(cljl);
			zdxzdcCljlDao.flush();
			
			Integer wcjls = zdxzdcCljlDao.getCount("select count(*) from zdxzdc_cljl t where t.zt<>1 and t.dcid='"+xzdc.getId()+"'");
			if(wcjls == 0){
				xzdc.setZxzt(1);
				zdxzdcDao.update(xzdc);
			}else{
				xzdc.setZxzt(0);
				zdxzdcDao.update(xzdc);
			}
		}
	}
	/**执行审核退回*/
	public void zxshth(Zdxzdc xzdc) {
		/*xzdc.setZxzt(0);
		zdxzdcDao.update(xzdc);*/
		if(StringUtils.notNull(xzdc.getJlid())){
			ZdxzdcCljl cljl = zdxzdcCljlDao.getById(xzdc.getJlid());
			if(cljl != null){
				cljl.setOptact(1);
				cljl.setZt(2);
				cljl.setWcqk(xzdc.getWcqk());
				cljl.setXygzjh(xzdc.getXygzjh());
				zdxzdcCljlDao.update(cljl);
			}
		}
	}
	public Page myTask(ZdxzdcQuery query) {
		return zdxzdcDao.myTask(query);
	}
	
	public Integer myTaskCount(Long userid) {
		return zdxzdcDao.myTaskCount(userid);
	}
	
	public InputStream searchExport(ZdxzdcQuery query) {
		List<Zdxzdc> list = zdxzdcDao.searchExport(query);
		if(list != null && list.size()>0){
			for(Zdxzdc dc : list){
				List<ZdxzdcCljl> cljlList = zdxzdcDao.searchWcjlById(dc.getId());
				dc.setCljlList(cljlList);
			}
		}
		ByteArrayOutputStream ops = genXls(list);
		InputStream ips = new ByteArrayInputStream(ops.toByteArray()); 
		return ips;
	}
	
	
	public ByteArrayOutputStream genXls(List<Zdxzdc> dclist){
		WritableWorkbook wwb;
		ByteArrayOutputStream ops = new ByteArrayOutputStream();
        try {
	        wwb = Workbook.createWorkbook(ops);
	        //设置单元格的文字格式
	        WritableFont wf = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
	        WritableCellFormat wcf = new WritableCellFormat(wf);
	        wcf.setVerticalAlignment(VerticalAlignment.CENTRE); 
	        wcf.setAlignment(Alignment.CENTRE); 
	        //填充数据的内容
	        Label label;
	        WritableSheet sheet=wwb.createSheet("重大行政督查事项", 0);
	        
	        sheet.setColumnView(1,20);
	        sheet.setColumnView(2,40);
	        sheet.setColumnView(3,40);
	        sheet.setColumnView(4,20);
	        sheet.setColumnView(5,40);
	        sheet.setColumnView(6,40);
	        //创建表头
			//sheet.mergeCells(0,0,0,0);   
	        label = new Label(0,0,"序号",wcf);   
	        sheet.addCell(label); 
	        label = new Label(1,0,"日期",wcf);   
	        sheet.addCell(label);
	        label = new Label(2,0,"相关工作项",wcf);   
	        sheet.addCell(label);
	        label = new Label(3,0,"具体要求与时间节点",wcf);   
	        sheet.addCell(label);
	        label = new Label(4,0,"分管领导",wcf);   
	        sheet.addCell(label);
	        label = new Label(5,0,"完成情况",wcf);   
	        sheet.addCell(label);
	        label = new Label(6,0,"下一步工作计划",wcf);   
	        sheet.addCell(label);
	        
	        int i=0;
	        for (Zdxzdc dc : dclist){
	            label = new Label(0,i+1,(i+1)+"",wcf);   //序号
            	sheet.addCell(label);
            	label = new Label(1,i+1,dc.getRqString());   		
	            sheet.addCell(label);
	            label = new Label(2,i+1,dc.getXggz());   		//相关工作项
	            sheet.addCell(label);
	            label = new Label(3,i+1,dc.getJtyq(),wcf);   	//具体要求
	            sheet.addCell(label);
	            label = new Label(4,i+1,dc.getFgldxm(),wcf);   			//分管领导
	            sheet.addCell(label);
	            label = new Label(5,i+1,dc.getWcqk(),wcf);   				//起始日期
	            sheet.addCell(label);
	            label = new Label(6,i+1,dc.getXygzjh(),wcf);   			//终止日期
	            sheet.addCell(label);
	            
	            i++;
	       }
	       wwb.write();
	       wwb.close();
       } catch (IOException e){
        	e.printStackTrace();
       } catch (RowsExceededException e){
        	e.printStackTrace();
       } catch (WriteException e){
        	e.printStackTrace();
       }
       return ops;
	}
}
